﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Service.Model;
using GtsServer.DAL;
using GtsServer.Model;
using GtsServer.Common;
using System.Data;
using System.Net;
using System.IO;
using System.Runtime.Serialization;

namespace GtsServer.Service.Controller
{
    public class BaseController
    {
        UserDAL userDal = new UserDAL();
        public BaseController() { }
        public BaseController(InitInfo info, out int goldNum, out string userAccount)
        {
            goldNum = 0;
            userAccount = string.Empty;
            InitUser(info, out goldNum, out  userAccount);
        }

        public static string Account(InitInfo info)
        {
            return info.user == null ? "" : info.user.sys_account ?? "";
        }

        public void InitUser(InitInfo user, out int goldNumout, out string userAccount)
        {
            goldNumout = 0;
            userAccount = string.Empty;
            User model = new User()
            {
                ChanceTypeID = 0,
                State = 1,
                IMSI = user.sim.imsi ?? "",
                PLSettingID = 0,
                Area = user.sim.area ?? "",
                CommunicationID = user.phone.pid,
                IMEI = user.sim.imei ?? "",
                IsRobot = false,
                Mac = user.phone.mac ?? "",
                Number = user.sim.number ?? "",
                Plmn = user.sim.plmn ?? "",
                RegisterTime = DateTime.Now,
                UserAccount = "",
                UserInfoID = 0,
                PrevRequestTime = DateTime.Now
            };
            DataSet ds = userDal.CheckUser(model);
            userAccount = ds.Tables[0].Rows[0]["userAccount"].ToString();

            #region
            ////TODO 目前数据库不同步 
            //#region 拉取1.0的用户数据
            ////是否已存在该用户  如果不存在则需要去1.0服务器拉取该用户数据，如果1.0服务器不存在该用户数据 则不发生改变
            //bool isAdd = false;
            //if (ds != null && ds.Tables.Count == 2 && ds.Tables[1].Rows.Count > 0)
            //{
            //    isAdd = Convert.ToInt32(ds.Tables[1].Rows[0][0]) == 1 ? true : false;
            //}

            ////添加的新用户  去1.0数据库拉取用户
            //if (isAdd)
            //{
            //    string imsi = user.user.uid;
            //    if (!string.IsNullOrEmpty(imsi))
            //    {
            //        string json = DownloadWeb("http://gtsuserinfo.auroraorbit.com/userinfo/get?imsi=" + imsi);

            //        if (!string.IsNullOrEmpty(json))
            //        {
            //            OldUserModel oldUser = JsonHelper.ParseJSON<OldUserModel>(json);
            //            string name = oldUser.Account;
            //            string pwd = oldUser.Password;
            //            int goldNum = oldUser.GoldNum;
            //            int eggNum = oldUser.EggNum;

            //            //将1.0数据库中的用户金币及菜单数量更新至2.0库中  加500金币
            //            goldNum += 500;
            //            userDal.EditFirstUserInitData(userAccount, name, pwd, goldNum, eggNum);

            //            goldNumout = goldNum;
            //        }
            //    }

            //    //ds = userDal.CheckFirstFruitUser(model.IMSI, model.IMEI, model.Mac);

            //    //if (ds != null && ds.Tables[0].Rows.Count > 0)
            //    //{
            //    //    bool isExist = Convert.ToInt32(ds.Tables[0].Rows[0]["isExist"]) == 1 ? true : false;

            //    //    //更新2.0库里的数据
            //    //    if (isExist)
            //    //    {
            //    //        string name = ds.Tables[0].Rows[0]["name"].ToString();
            //    //        string pwd = ds.Tables[0].Rows[0]["pwd"].ToString();
            //    //        int goldNum = ds.Tables[0].Rows[0]["goldNum"] == null ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["goldNum"]);
            //    //        int eggNum = ds.Tables[0].Rows[0]["eggNum"] == null ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["eggNum"]);

            //    //        //将1.0数据库中的用户金币及菜单数量更新至2.0库中  加500金币
            //    //        goldNum += 500;
            //    //        userDal.EditFirstUserInitData(userAccount, name, pwd, goldNum, eggNum);

            //    //        goldNumout = goldNum;
            //    //    }
            //    //}
            //}
            //#endregion
            #endregion
        }

        /// <summary>
        /// 获取用户imsi
        /// </summary>
        /// <param name="imsi"></param>
        /// <param name="mac"></param>
        /// <param name="imei"></param>
        /// <returns></returns>
        private string GetImsi(string imsi, string mac, string imei)
        {
            string returnImsi = imsi;

            if (imsi.Length > 0 && imsi.Length < 15)
                returnImsi = imsi + "_" + mac;
            else if (string.IsNullOrEmpty(imsi) && !string.IsNullOrEmpty(imei))
                returnImsi = imei + "_" + mac;
            else if (string.IsNullOrEmpty(imei))
                returnImsi = mac;

            return returnImsi;
        }


        string DownloadWeb(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            string content = string.Empty;
            using (Stream resStream = response.GetResponseStream())
            {
                Encoding encoding = Encoding.UTF8;
                StreamReader streamReader = new StreamReader(resStream, encoding);
                content = streamReader.ReadToEnd();
            }
            return content;
        }
    }

    [DataContract]
    public class OldUserModel
    {
        [DataMember]
        public string Imsi;
        [DataMember]
        public string Account;
        [DataMember]
        public int GameNum;
        [DataMember]
        public int GoldNum;
        [DataMember]
        public int EggNum;
        [DataMember]
        public string FirstPlayDate;
        [DataMember]
        public string LastPlayDate;
        [DataMember]
        public string Password;
        [DataMember]
        public string RealName;
        [DataMember]
        public string QQ;
        [DataMember]
        public string Email;
        [DataMember]
        public string Mobile;
        [DataMember]
        public string Address;
    }
}
